// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-08 18:36
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;

import com.DataStruct.Tree.TwoBinaryTree.TreeNode;

public class IsSymmetric {
    /**
     * 判断二叉树是否是对称二叉树
     */
    public boolean isSymmetric(TreeNode root) {
        // 遍历的时候使用对称指针
        if (root == null) {
            return true;
        }
        return search(root.left, root.right);
    }

    /**
     * 使用对称指针判断二叉树是否是对称二叉树
     *
     * @param left  左指针
     * @param right 右指针
     * @return 返回值
     */
    private boolean search(TreeNode left, TreeNode right) {
        boolean L = true, R = true;
        if (left != null && right != null) {
            if (left.val != right.val) {
                return false;
            }
            L = search(left.right, right.left);
            R = search(left.left, right.right);
        } else if (left != null || right != null) {
            return false;
        }

        return L && R;
    }
}
